{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用langchain框架调用通义千问"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、使用llm的predict的简单使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'在中国，给孩子取名通常会结合姓氏、生辰八字、五行等因素，以寄寓美好的寓意。对于龙年出生、郎姓的男性宝宝，我们可以考虑一些寓意吉祥、力量或者智慧的名字。以下是一些提议：\\n\\n1. 郎龙瑞：龙代表他的生肖，瑞意为吉祥如意。\\n2. 郎龙翔：翔意为翱翔，寓意他能像龙一样自由翱翔，有远大的前程。\\n3. 郎龙智：智代表智慧，希望他聪明伶俐，充满智慧。\\n4. 郞龙杰：杰意为杰出，期待他能成为一个出类拔萃的人。\\n5. 郞龙瀚：瀚字有广大无边的意思，寓意他有广阔的胸怀和知识领域。\\n\\n以上只是一些基本的建议，具体的名字还需要根据宝宝的生辰八字和家长的个人喜好来定。'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"]= \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "\n",
    "llm = Tongyi(DASHSCOPE_API_KEY = \"sk-9d8f1914800e497f8717144e860f99bc\")\n",
    "llm.predict(\"请给龙年出生、郎姓的男性宝宝起名字\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2、使用提示模板、链式模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\"石头\"。这个名字简单朴实，寓意坚硬顽强，象征孩子有坚韧不拔的精神和健康的身体。'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "\n",
    "prompt = PromptTemplate.from_template(\"给我一个很土但是听起来好养活{对象}小名\")\n",
    "prompt.format(对象=\"小猫\")\n",
    "\n",
    "llm = Tongyi(DASHSCOPE_API_KEY = \"sk-9d8f1914800e497f8717144e860f99bc\")\n",
    "chain = LLMChain(llm= llm,prompt= prompt)\n",
    "chain.run(\"小男孩\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "input_variables=['question'] template='\\n    Question:{question}\\n    Answer:可以逐步思考并详细回答这个问题\\n    '\n",
      "1. 确定主题和目标：首先，你需要确定你的视频内容是什么。这可以是教程、娱乐、新闻、个人日记、产品展示等。同时，要明确你的目标观众是谁，这将帮助你决定视频的风格、语言和内容深度。\n",
      "\n",
      "2. 编写剧本或大纲：即使是对口头叙述的视频，有一个清晰的剧本或大纲也能帮助你保持结构和连贯性。列出你要涵盖的主要点，以及每个部分的大概时间。\n",
      "\n",
      "3. 拍摄设备准备：你可以使用专业的摄像机，也可以用手机拍摄。确保设备稳定，光线充足，音质清晰。如果需要，可以购买三脚架、麦克风和补光灯等辅助设备。\n",
      "\n",
      "4. 拍摄视频：根据你的剧本或大纲开始拍摄。尝试多角度拍摄，以增加视频的视觉吸引力。记住，多次拍摄同一个场景，以便在后期编辑时有更多选择。\n",
      "\n",
      "5. 视频编辑：使用视频编辑软件（如Adobe Premiere, Final Cut Pro, iMovie等）进行剪辑，添加过渡效果、音乐、字幕和特效。保持视频节奏流畅，避免过于冗长。\n",
      "\n",
      "6. 配乐和声音处理：选择合适的背景音乐，确保音量适中，与视频内容相匹配。如果需要，可以进行配音或添加声音效果。\n",
      "\n",
      "7. 导出和优化：根据目标视频网站的要求导出视频，例如分辨率、帧率和文件大小。优化视频质量，使其在网络上传输时既清晰又快速。\n",
      "\n",
      "8. 创建标题和描述：给视频起一个吸引人的标题，简短但能准确反映视频内容。写一段详细的描述，包含关键词，有助于搜索引擎优化。\n",
      "\n",
      "9. 选择标签：添加相关标签，使你的视频更容易被搜索到。考虑使用热门关键词和特定话题的标签。\n",
      "\n",
      "10. 上传和分享：将视频上传到你选择的视频平台，如YouTube、Vimeo或TikTok等。分享到社交媒体，邀请朋友观看并提供反馈。\n",
      "\n",
      "11. 监控和互动：发布后，关注视频的观看次数、评论和点赞数。对观众的评论进行回复，建立社区，这有助于提高视频的可见度。\n",
      "\n",
      "通过以上步骤，你可以创建并成功上传一个高质量的视频。记住，持续实践和学习会使你的视频制作技能不断提高。\n"
     ]
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.chains import LLMChain\n",
    "import os\n",
    "os.environ[\"DASHSCOPE_API_KEY\"]= \"sk-9d8f1914800e497f8717144e860f99bc\"\n",
    "\n",
    "template = \"\"\"\n",
    "    Question:{question}\n",
    "    Answer:可以逐步思考并详细回答这个问题\n",
    "    \"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template=template,\n",
    "    input_variables=[\"questions\"]\n",
    ")\n",
    "\n",
    "print(prompt)\n",
    "\n",
    "llm = Tongyi(DASHSCOPE_API_KEY = \"sk-9d8f1914800e497f8717144e860f99bc\")\n",
    "llm_chain = LLMChain(prompt=prompt,llm=llm)\n",
    "question = \"怎样才能做好一个视频并且上传到视频网站?\"\n",
    "res = llm_chain.run(question)\n",
    "print(res)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3、代理人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\DevelopEnvironment\\python\\python 3.9.9\\lib\\site-packages\\langchain_core\\_api\\deprecation.py:117: LangChainDeprecationWarning: The function `initialize_agent` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use Use new agent constructor methods like create_react_agent, create_json_agent, create_structured_chat_agent, etc. instead.\n",
      "  warn_deprecated(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3m 需要用计算器计算25的3.5次方。\n",
      "Action: Calculator\n",
      "Action Input: 25^(3.5)\u001b[0m\n",
      "Observation: \u001b[36;1m\u001b[1;3mAnswer: 78125.0\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3m 我现在知道了最终答案。\n",
      "Final Answer: 25的3.5次方是78125。\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'25的3.5次方是78125。'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "from langchain.agents import initialize_agent\n",
    "from langchain.agents import load_tools\n",
    "from langchain.agents import AgentType\n",
    "\n",
    "llm = Tongyi(DASHSCOPE_API_KEY = \"sk-9d8f1914800e497f8717144e860f99bc\")\n",
    "# 添加代理人工具\n",
    "tools = load_tools([\"llm-math\"],llm=llm)\n",
    "# 初始化代理人\n",
    "agent = initialize_agent(tools, llm, agent = AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose = True)\n",
    "# 执行代理人\n",
    "agent.run(\"25的3.5次方是多少?\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4、聊天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "\n",
      "Human: 你好呀\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好！今天过得怎么样？有没有什么新鲜事情或者需要我帮助解答的问题呢？我在这里随时准备为你提供信息。'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.llms import Tongyi\n",
    "from langchain.chains import ConversationChain\n",
    "llm = Tongyi(DASHSCOPE_API_KEY = \"sk-9d8f1914800e497f8717144e860f99bc\")\n",
    "conversation = ConversationChain(llm=llm,verbose=True)\n",
    "conversation.run(\"你好呀\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "Human: 你好呀\n",
      "AI: 你好！今天过得怎么样？有没有什么新鲜事情或者需要我帮助解答的问题呢？我在这里随时准备为你提供信息。\n",
      "Human: 我今天领养了一只小猫\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'太棒了！领养宠物是一件非常有爱心的事情。小猫是什么品种的呢？是刚出生不久的小奶猫，还是已经稍微大一点的呢？给它取名字了吗？照顾小猫需要注意很多事情，比如定期喂食、清理猫砂盆、保证它有足够的休息和玩耍时间，还有确保它得到必要的兽医检查。如果你有任何关于照顾小猫的问题，我会尽力帮你解答。'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.run(\"我今天领养了一只小猫\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
      "Prompt after formatting:\n",
      "\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
      "\n",
      "Current conversation:\n",
      "Human: 你好呀\n",
      "AI: 你好！今天过得怎么样？有没有什么新鲜事情或者需要我帮助解答的问题呢？我在这里随时准备为你提供信息。\n",
      "Human: 我今天领养了一只小猫\n",
      "AI: 太棒了！领养宠物是一件非常有爱心的事情。小猫是什么品种的呢？是刚出生不久的小奶猫，还是已经稍微大一点的呢？给它取名字了吗？照顾小猫需要注意很多事情，比如定期喂食、清理猫砂盆、保证它有足够的休息和玩耍时间，还有确保它得到必要的兽医检查。如果你有任何关于照顾小猫的问题，我会尽力帮你解答。\n",
      "Human: 你能为这只小猫起个名字吗\n",
      "AI:\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'当然可以！给你的小猫起名字是一件很有趣的事情。考虑到我并不了解它的特征或性格，我可以提供一些常见的猫咪名字供你选择，比如“咪咪”、“小白”、“橘子”、“球球”、“花花”或者“闪电”。如果你能告诉我它的一些特点，我还可以根据那些来提议更个性化的名字。'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conversation.run(\"你能为这只小猫起个名字吗\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "eec67444d00d22f71b39bd6bbad710a72749a23d029a63a184fb88e8d46e38e1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
